//
//  CategoryViewController.m
//  openEye
//
//  Created by 徐悟源 on 16/3/31.
//  Copyright © 2016年 Wuyuan. All rights reserved.
//

#import "CategoryViewController.h"
#import "Categories.h"
#import "CategoryCollectionViewCell.h"
#import "CategoryTimeAndShareViewController.h"

@interface CategoryViewController ()

@property (nonatomic , strong) NSArray *categories;

@end

static NSString *ID = @"cellID";

@implementation CategoryViewController


- (NSArray *)categories
{
    if(!_categories)
    {
        self.categories = [[NSArray alloc] init];
    }
    
    return _categories;
}

- (instancetype)init
{
    UICollectionViewFlowLayout *layout = [self settingLayout];
    if(self = [super initWithCollectionViewLayout:layout])
    {
        [self loadViewData];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self loadViewData];
    
    [self.collectionView registerClass:[CategoryCollectionViewCell class] forCellWithReuseIdentifier:ID];
    
    [self.collectionView setBackgroundColor:[UIColor whiteColor]];
    
    self.collectionView.backgroundColor = WYColorWithAlpha(240, 240, 240, 0.9);
    
    
}

- (void)addNavigationItem
{
    // 设置标题
    NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
    
    attrs[NSFontAttributeName] = Font_EnglishFont(20);
    
    // 加了属性的string
    NSMutableAttributedString *title = [[NSMutableAttributedString alloc] initWithString:@"Wuyuan" attributes:attrs];
    
    UILabel *titleLabel = [[UILabel alloc] init];
    titleLabel.attributedText = title;
    [titleLabel setBounds:CGRectMake(0, 0, title.size.width, title.size.height)];
    self.navigationItem.titleView = titleLabel;
    
}

/** 加载数据 */
- (void)loadViewData
{
    [[AFHTTPSessionManager manager] GET:[Categorie toPath] parameters:[Categorie toParams] progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        _categories = [Categorie mj_objectArrayWithKeyValuesArray:responseObject];
        
        [self.collectionView reloadData];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [SVProgressHUD showErrorWithStatus:@"网络错误"];
    }];
}

#pragma mark - 设置布局
- (UICollectionViewFlowLayout *)settingLayout
{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    
    // 设置间距
    CGFloat space = 3;
    layout.minimumInteritemSpacing = space;
    layout.minimumLineSpacing = space;
    layout.sectionInset = UIEdgeInsetsMake(space, space, 0, space);
    
    // 设置Item的size
    CGFloat itemW = (WYScreenSize.width - space) / 2 - space;
    CGFloat itemH = itemW;
    
    layout.itemSize = CGSizeMake(itemW , itemH);
    
    return layout;
}

#pragma mark <UICollectionViewDataSource>

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {

    return self.categories.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    CategoryCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
    
    if(cell == nil)
    {
        cell = [[CategoryCollectionViewCell alloc] init];
    }
    
    cell.category = self.categories[indexPath.row];
    
    return cell;
}

#pragma mark <UICollectionViewDelegate>

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    CategoryTimeAndShareViewController *vc = [[CategoryTimeAndShareViewController alloc] init];
    
    vc.category = self.categories[indexPath.row];
    
    [self.navigationController pushViewController:vc animated:YES];
}
@end
